//
// Created by kevin on 25-9-3.
//
// example.c
#include "socket_client.h"
#include <stdio.h>

void on_connected() {
    printf("Connected to server!\n");
}

void on_disconnected() {
    printf("Disconnected from server!\n");
}

void on_message_received(const char* data, size_t length) {
    printf("Received message: %.*s\n", (int)length, data);
}

void on_error(const char* error_msg) {
    printf("Error: %s\n", error_msg);
}

int main() {
    connection_config_t config = {
        .server_ip = "127.0.0.1",
        .server_port = 9213,
        .mode = UDP_MODE,
        .heartbeat_interval = 10,
        .reconnect_interval = 5
    };

    event_callbacks_t callbacks = {
        .on_connected = on_connected,
        .on_disconnected = on_disconnected,
        .on_message_received = on_message_received,
        .on_error = on_error
    };

    socket_client_t* client = socket_client_create(&config, &callbacks);
    if (!client) {
        printf("Failed to create socket client\n");
        return -1;
    }

    if (socket_client_connect(client) != 0) {
        printf("Failed to connect\n");
        socket_client_destroy(client);
        return -1;
    }

    // 发送消息
    const char* message = "Hello, Server!";
    socket_client_send(client, message, strlen(message));

    // 等待一段时间
    Sleep(100000);
    printf("close client\n");

    // 断开连接
    socket_client_disconnect(client);
    socket_client_destroy(client);

    return 0;
}